<?
include_once("../arc/ARC2.php");

set_time_limit(600);

//setup sparql query thing..
$config = array(
  /* db */
  'db_name' => 'test',
  'db_user' => 'root',
  'db_pwd' => '',
  /* store */
  'store_name' => 'yalda',
);
$store = ARC2::getStore($config);
if (!$store->isSetUp()) {
  $store->setUp();
}
//define remote endpoint
$config = array('remote_store_endpoint' => 'http://dblp.l3s.de/d2r/sparql');
$store = ARC2::getRemoteStore($config);

mysql_connect("dev.ivaldi.nl", "root", "w8woord-ivaldi") or die(mysql_error());
mysql_select_db("publications") or die(mysql_error());

$query = mysql_query("SELECT name, id FROM authors_dn");
$found = 0;
$count = 0;
while($res = mysql_fetch_array($query)){
	$count++;

	/* list names */
	$q = '
	  PREFIX foaf: <http://xmlns.com/foaf/0.1/>  

	  SELECT ?x WHERE {
		?x foaf:name "'.$res['name'].'"		
		}
	';
	$r = '';
	if ($rows = $store->query($q, 'rows')) {
	  foreach ($rows as $row) {
		$found++;		
		mysql_query("INSERT INTO sameas (id, uri, author_id) VALUES ('', '".$row['x']."', '".$res['id']."')");
		echo '<li>'. $count . '. ' . $row['x'] . '</li>';
	  }
	}

	//echo $r ? '<ul>' . $r . '</ul>' : 'no named persons found';

}

echo '<h1>'.$found.' matchings! and '.$count.' rows done</h1>';

//list names localhost
/*
#
$config = array('remote_store_endpoint' => 'http://localhost:2020/sparql');
#
$store = ARC2::getRemoteStore($config);
$q = '
  PREFIX foaf: <http://xmlns.com/foaf/0.1/>  

  SELECT ?x WHERE {
	?x foaf:name ?name
	FILTER regex(str(?x), "houb")
	}
';
$r = '';
if ($rows = $store->query($q, 'rows')) {
  foreach ($rows as $row) {
    $r .= '<li>' . $row['x'] . '</li>';
  }
}

echo $r ? '<ul>' . $r . '</ul>' : 'no named persons found';*/


?>